﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace PedesTrainProject.Data
{
    public class Serialiazer
    {
        public void SerializeGrid(string filename, Grid.Grid gridToSerialize)
        {
            Stream stream = File.Open(filename, FileMode.Create);
            BinaryFormatter bFormatter = new BinaryFormatter();
            bFormatter.Serialize(stream, gridToSerialize);
            stream.Close();
        }

        public Grid.Grid DeSerializeGrid(string filename)
        {
            Grid.Grid gridToDeserialize;
            Stream stream = File.Open(filename, FileMode.Open);
            BinaryFormatter dFormatter = new BinaryFormatter();
            gridToDeserialize = (Grid.Grid)dFormatter.Deserialize(stream);
            stream.Close();
            return gridToDeserialize;
        }

        public void SerializeStatistics(string filename, List<Statistics> statisticsToSerialize)
        {
            Stream stream = File.Open(filename, FileMode.Create);
            BinaryFormatter bFormatter = new BinaryFormatter();
            bFormatter.Serialize(stream, statisticsToSerialize);
            stream.Close();
        }

        public List<Statistics> DeSerializeStatistics(string filename)
        {
            List<Statistics> statisticsToDeserialize;
            Stream stream = File.Open(filename, FileMode.Open);
            BinaryFormatter dFormatter = new BinaryFormatter();
            statisticsToDeserialize = (List<Statistics>)dFormatter.Deserialize(stream);
            stream.Close();
            return statisticsToDeserialize;
        }
        
        public void SerializeSimulation(string filename, Core.Simulation simulationToSerialize)
        {
            Stream stream = File.Open(filename, FileMode.Create);
            BinaryFormatter bFormatter = new BinaryFormatter();
            bFormatter.Serialize(stream, simulationToSerialize);
            stream.Close();
        }

        public Core.Simulation DeSerializeSimulation(string filename)
        {
            Core.Simulation simulationToDeserialize;
            Stream stream = File.Open(filename, FileMode.Open);
            BinaryFormatter bFormatter = new BinaryFormatter();
            simulationToDeserialize = (Core.Simulation)bFormatter.Deserialize(stream);
            stream.Close();
            return simulationToDeserialize;
        }
    }
}
